//
//  ViewController.swift
//  Location
//
//  Created by Lxin on 2018/9/19.
//  Copyright © 2018年 Lxin. All rights reserved.
//

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var locationLabel: UILabel!
    @IBOutlet weak var locationTitle: UILabel!
    var manager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        manager.delegate = self
        manager.requestWhenInUseAuthorization()
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.distanceFilter = 1.0
        manager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let currentLocation = locations.last
        locationLabel.text = "纬度:\(currentLocation?.coordinate.latitude ?? 0.0),经度:\(currentLocation?.coordinate.longitude ?? 0.0)"
        let geoCoder = CLGeocoder()
        geoCoder.reverseGeocodeLocation(currentLocation!) { (placemarks, error) in
            if let places = placemarks {
                let placemark = places.first
                print("当前国家 - \(placemark?.country ?? "")")
                print("当前城市 - \(placemark?.locality ?? "")")
                print("当前位置 - \(placemark?.subLocality ?? "")")
                print("当前街道 - \(placemark?.thoroughfare ?? "")")
                print("具体地址 - \(placemark?.name ?? "")")
                self.locationTitle.text = "\(placemark?.country ?? "")\(placemark?.locality ?? "")\(placemark?.subLocality ?? "")\(placemark?.thoroughfare ?? "")\(placemark?.name ?? "")"
            }
        }
    }
}

